package com.gwz.sys.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gwz.sys.constast.SYSConstast;
import com.gwz.sys.domain.Permission;
import com.gwz.sys.service.PermissionService;
import com.gwz.sys.service.RoleService;
import com.gwz.sys.utils.DataGridView;
import com.gwz.sys.utils.ResultObj;
import com.gwz.sys.utils.TreeNode;
import com.gwz.sys.vo.PermissionVo;
import com.gwz.sys.vo.RoleVo;

@RestController
@RequestMapping("role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private PermissionService permissionService;
	
	@RequestMapping("loadAllRole")
	public DataGridView loadAllRole(RoleVo roleVo) {
		return roleService.queryAllRole(roleVo);
	}
	
	
	@RequestMapping("addRole")
	public ResultObj addRole(RoleVo roleVo) {
		ResultObj resultObj = new ResultObj(200, "添加成功");
		try {
			roleService.addRole(roleVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("添加失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	@RequestMapping("updateRole")
	public ResultObj updateRole(RoleVo roleVo) {
		ResultObj resultObj = new ResultObj(200, "修改成功");
		try {
			roleService.updateRole(roleVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("修改失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	@RequestMapping("deleteRole")
	public ResultObj deleteRole(RoleVo roleVo) {
		ResultObj resultObj = new ResultObj(200, "删除成功");
		try {
			roleService.deleteRole(roleVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("删除失败");
			e.printStackTrace();
			return resultObj;
		}
	}
	
	@RequestMapping("loadRolePermissionSelect")
	public List<TreeNode> loadRolePermissionSelect(RoleVo roleVo){
		PermissionVo permissionVo = new PermissionVo();
		permissionVo.setAvailable(SYSConstast.SYS_AVAILABLE_TRUE);
		List<Permission> allPermission = permissionService.queryAllPermissionForList(permissionVo);
		List<Permission> currentRolePermissions = permissionService.queryPermisionByRoleIdForList(permissionVo,roleVo.getId());
		List<TreeNode> treeNodes = new ArrayList<>();
		for (Permission p1 : allPermission) {
			Boolean checked = false;
			for (Permission p2 : currentRolePermissions) {
				 	if (p1.getId() == p2.getId()) {
						checked = true;
						break;
					}
			}
			Boolean open = p1.getOpen() == SYSConstast.SYS_OPEN_TRUE ? true : false;
			Boolean isParent = false;
			if(p1.getType().equals(SYSConstast.PERMISSION_TYPE_MEUN)) {
				isParent=true;
			}
			treeNodes.add(new TreeNode(p1.getId(), p1.getPid(), p1.getName(), open, isParent, checked));
		}
		return treeNodes;
	}
	
	
	//保存角色和权限的关系
	@RequestMapping("saveRolePermission")
	public ResultObj saveRolePermission(RoleVo roleVo) {
		ResultObj resultObj = new ResultObj(200, "分配成功");
		try {
			roleService.saveRolePermission(roleVo);
			return resultObj;
		} catch (Exception e) {
			resultObj.setCode(-1);
			resultObj.setMsg("分配失败");
			e.printStackTrace();
			return resultObj;
		}
	}
}
